package com.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.ysh.gsm.vos.SingleReportSerice;

public class SingleDataCalc {

	public static List<SingleReportSerice> calTotal(
			List<? extends DbMRData> source, long total) {
		if (total == 0 || source == null || source.size() == 0) {
			return null;
		}
		List<SingleReportSerice> result = new ArrayList<SingleReportSerice>();
		long sum = 0;
		Collections.sort(source, new CustomComparator());
		int len = source.size();
		for (int i = 0; i < len; i++) {
			sum += source.get(i).getCount();
			if (i == len - 1) {
				result.add(new SingleReportSerice(source.get(i).getVal(),
						(double) sum * 100 / total));
				break;
			}
			if (source.get(i).getVal() != source.get(i + 1).getVal()) {
				result.add(new SingleReportSerice(source.get(i).getVal(),
						(double) sum * 100 / total));
			
			}

		}
		return result;
	}

	private static class CustomComparator implements Comparator<DbMRData> {

		public int compare(DbMRData o1, DbMRData o2) {
			return (o1.getVal() > o2.getVal() ? 1 : (o1.getVal() == o2
					.getVal() ? 0 : -1));

		}
	}

}
